/**
 * 树扩展类
 * wendy
 *
 * */
layui.define(["jquery", "layer", 'tree'], function (exports) {
    var model_name = "myTree";
    var $ = layui.jquery, layer = layui.layer, this_tree = layui.tree;

    var obj = {
        v: '1.0.0',
        tree: undefined,
        /////树型点击高量显示
        select: function (o, elem) {
            $(elem).parent().parent().find(".tree-selectd").removeClass('tree-selectd');
            $(o.elem).find(".layui-tree-txt").first().addClass("tree-selectd");
        }
        , init: function (initid, elem) {
            var txt = '';
            var txtlist = elem != undefined ? $(elem).find("div [data-id]") : $("div [data-id]");
            txtlist.each(function (obj) {
                if (this.getAttribute("data-id") == initid) {
                    txt = ($(this).text());
                    $(this).find(".layui-tree-txt").first().addClass("tree-selectd");
                    //展开所有上层父节点
                    $.each($(this).find('.tree-selectd'), function (index, elem) {
                        elem = $(elem);
                        elem.parents('.layui-tree-set').each(function (i, item) {
                            if (!$(item).hasClass('layui-tree-spread')) {
                                $(item).find('.layui-tree-iconClick:first').click();
                            }
                        });
                    });
                }
            })
            return txt;
        }
        , render: function (options) {
            var defaults = {
                elem: '#tree_1',
                showCheckbox: false,
                id: 'tree1',
                isJump: true,
                jsOpen: true
            };
            var options = $.extend(defaults, options);

            var doneCallback = options.click;
            options.click = function (o) {
                var loadindex = layer.load(2);
                if (!o.data.turn) {
                } else {
                    obj.select(o, options.elem);
                    o.options = options;
                    if (doneCallback) {
                        doneCallback(o);
                    }
                }
                layer.close(loadindex);
            }

            tree = this_tree.render(options);
            if (options.url) {
                $.ajaxSetup({async: false});//同步执行
                layui.$.get(options.url, {}, function (result) {
                    // console.info(result);
                    options.data = result.data.tree;
                    options.init = result.data.init;
                    tree = this_tree.render(options);
                    if (options.init) {
                        obj.init(options.init);
                    }
                });
            }

            if (options.init) {
                obj.init(options.init);
            }

            this.reload = function () {
                tree.reload({data: []});
                var index = layer.load();
                if (options.url) {
                    layui.$.get(options.url, {}, function (result) {
                        options.init = result.data.init;
                        tree.reload({data: result.data.tree});
                        if (options.init) {
                            myTree.init(options.init);
                        }
                        layer.close(index);
                    });
                }
            };
            return this;
        }
    }
    exports(model_name, obj);
});